#include "cxitagwidget.h"
#include <QLabel>
#include <QToolButton>
#include <QHBoxLayout>

CxiTagWidget::CxiTagWidget(const QString& text, QWidget* parent)
	: QWidget(parent)
{
	auto* layout = new QHBoxLayout(this);
	layout->setContentsMargins(0, 0, 0, 0);
	layout->setSpacing(0);

	m_label = new QLabel(text, this);
	layout->addWidget(m_label);
	if(text != "..."){
		m_closeBtn = new QToolButton(this);
		m_closeBtn->setText("✕");             // 或使用图标
		m_closeBtn->setAutoRaise(true);
		m_closeBtn->setFixedSize(16, 16);
		layout->addWidget(m_closeBtn);
		setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);

		connect(m_closeBtn, &QToolButton::clicked, this, [this]() {

			emit tagRemoved(m_label->text());
		});
	}
}

QString CxiTagWidget::text() const
{
	return m_label ? m_label->text() : QString();
}

void CxiTagWidget::setText(const QString& t)
{
	if (m_label) m_label->setText(t);
}
